package com.lp.biz.train.model.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.lp.biz.train.model.dto.TrainMembersDto;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static com.lp.common.constant.CommonConstant.DATE_TIME;
import static com.lp.common.constant.CommonConstant.TIMEZONE;
import static com.lp.common.constant.TrainConstant.*;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-05-20 20:17:08
 */
@Data
public class TrainMembers {

    private Integer id;

    private String nickName;

    private String phone;

    @JsonFormat(timezone = TIMEZONE, pattern = DATE_TIME)
    private LocalDateTime signUpAt;

    private String className;

    private Integer classStatus;

    private Integer classId;

    public static TrainMembers builder(TrainMembersDto dto) {
        LocalDate now = LocalDate.now();
        TrainMembers vo = new TrainMembers();
        vo.setId(dto.getId());
        vo.setNickName(dto.getNickName());
        vo.setPhone(dto.getPhone());
        vo.setSignUpAt(dto.getSignUpAt());
        vo.setClassName(dto.getClassName());
        vo.setSignUpAt(dto.getCreateAt());
        vo.setClassId(dto.getClassId());
        if (dto.getClassId() != null) {
            if (dto.getStartAt() == null || dto.getEndAt() == null) {
                vo.setClassStatus(CLASS_NOT_START);
            } else {
                if (now.isBefore(dto.getStartAt())) {
                    vo.setClassStatus(CLASS_NOT_START);
                } else if (now.isAfter(dto.getEndAt())) {
                    vo.setClassStatus(CLASS_ENDED);
                } else {
                    vo.setClassStatus(CLASS_STARTED);
                }
            }
        }
        return vo;
    }
}
